package 异常的学习;

import java.util.Scanner;

//编写一个复数类，可以进行复数加法和减法运算。编写一个包含main方法的类测试该复数类。要求该复数类至少包含一个无参的构造方法和一个带参的构造方法；数据成员包括复数的实部和虚部，为double类型；包括两个方法，分别实现复数的加法和减法运算。测试代码如下：
public class PTA_other1 {
    public static class Complex{
        private double realPart;
        private double imaginaryPart;
        public Complex(){
            this.realPart=0;
            this.imaginaryPart=0;
        }
        public void setRealPart(double realPart){
            this.realPart=realPart;
        }
        public void setImaginaryPart(double imaginaryPart){
            this.imaginaryPart=imaginaryPart;
        }
//        add和sub直接输出结果：例如1.0+2.0i
        public String add(Complex b){
            double realPart=this.realPart+b.realPart;
            double imaginaryPart=this.imaginaryPart+b.imaginaryPart;
            return realPart+"+"+imaginaryPart+"i";
        }
        public String sub(Complex b){
            double realPart=this.realPart-b.realPart;
            double imaginaryPart=this.imaginaryPart-b.imaginaryPart;
            return realPart+"+"+imaginaryPart+"i";
        }
        public String toString(){
            return this.realPart+"+"+this.imaginaryPart+"i";
        }
    }
    public static void main(String [] args){
        Complex a=new Complex();
        Complex b=new Complex();
        Scanner in=new Scanner(System.in);
        a.setRealPart(in.nextDouble());
        a.setImaginaryPart(in.nextDouble());
        b.setRealPart(in.nextDouble());
        b.setImaginaryPart(in.nextDouble());
        System.out.println(a);
        System.out.println(b);
        System.out.println(a.add(b));
        System.out.println(a.sub(b));
    }
}
